package com.zhku.service.impl;

import com.zhku.entity.User;
import com.zhku.entity.UserInfo;
import com.zhku.mapper.UserInfoMapper;
import com.zhku.mapper.UserMapper;
import com.zhku.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author dewey
 * @since 2023-03-31
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    // 注入UserInfoMapper
    @Autowired
    UserInfoMapper userInfoMapper;

    @Override
    @Transactional
    public boolean saveUserAndInfo(User user, UserInfo userInfo) {
        // 先保存user，
        boolean one = this.save(user);
        // 因为业务逻辑是user的id，为userinfo的user_id, 所以要手动注入
        userInfo.setUserId(user.getId());
        // 保存userinfo
        int insert = userInfoMapper.insert(userInfo);

        return one && (insert > 0);
    }

    // 重写主键查询方法（user，userInfo信息）
    @Override
    public User getById(Serializable id) {
        // 账号信息
        User user = super.getById(id);
        // 设置userInfo属性
        user.setUserInfo(userInfoMapper.selectById(user.getId()));
        return user;
    }

}
